/*
 * @lc app=leetcode.cn id=641 lang=java
 *
 * [641] 设计循环双端队列
 */

// @lc code=start
class MyCircularDeque {

    int[] queueArr ;
    int frontIndex = 0;
    int endIndex = 1;//特殊关注，不能是0，要不第一次inserFront及inserLast时，它们指向就会有重叠
    int count = 0;

    /** Initialize your data structure here. Set the size of the deque to be k. */
    public MyCircularDeque(int k) {
        queueArr = new int[k];
    }
    
    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    public boolean insertFront(int value) {
        if(isFull()) return false;
        
        queueArr[frontIndex] = value;

        frontIndex --;
        if(frontIndex == -1){
            frontIndex = queueArr.length-1;
        }

        count++;
        return true;
    }
    
    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    public boolean insertLast(int value) {
        if(isFull()) return false;
        
        queueArr[endIndex] = value;
        
        endIndex ++;
        if(endIndex > queueArr.length-1){
            endIndex = endIndex % queueArr.length;
        }

        count++;
        return true;
    }
    
    /** Deletes an item from the front of Deque. Return true if the operation is successful. */
    public boolean deleteFront() {
        if(isEmpty()) return false;

        frontIndex ++;
        if(frontIndex>queueArr.length-1){
            frontIndex = frontIndex % queueArr.length;
        }

        count--;
        return true;
    }
    
    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    public boolean deleteLast() {
        if(isEmpty()) return false;

        endIndex --;
        if(endIndex  == -1){
            endIndex = queueArr.length - 1;
        }

        count--;
        return true;
    }
    
    /** Get the front item from the deque. */
    public int getFront() {
        if(isEmpty()) return -1;

        int curFrontIndex = frontIndex + 1;
        if(curFrontIndex > queueArr.length-1){
            curFrontIndex = curFrontIndex % queueArr.length;
        }

        return queueArr[curFrontIndex];
    }
    
    /** Get the last item from the deque. */
    public int getRear() {
        if(isEmpty()) return -1;

        int curEndIndex = endIndex - 1;
        if(curEndIndex == -1){
            curEndIndex = queueArr.length - 1;
        }

        return queueArr[curEndIndex];
    }
    
    /** Checks whether the circular deque is empty or not. */
    public boolean isEmpty() {
        return count == 0;
    }
    
    /** Checks whether the circular deque is full or not. */
    public boolean isFull() {
        return count == queueArr.length;
    }
}

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque obj = new MyCircularDeque(k);
 * boolean param_1 = obj.insertFront(value);
 * boolean param_2 = obj.insertLast(value);
 * boolean param_3 = obj.deleteFront();
 * boolean param_4 = obj.deleteLast();
 * int param_5 = obj.getFront();
 * int param_6 = obj.getRear();
 * boolean param_7 = obj.isEmpty();
 * boolean param_8 = obj.isFull();
 */
// @lc code=end

